<template>
	<view class="login">
		<uni-nav-bar dark :fixed="true" :border="false" background-color="#FFFFFF" status-bar title="浩泽数控" color="#000" />
		<view class="login-container">
			<u--form :model="form" ref="uForm" style="height: 500rpx;">
				<u-form-item label="账号" prop="empno">
					<u-input v-model="form.empno" border="bottom" 
						placeholder="账号"/>
				</u-form-item>
				<u-form-item label="密码" prop="password">
					<u-input password v-model="form.password" border="bottom" 
						placeholder="密码" />
				</u-form-item>
			</u--form>
			<view style="width: 80%; margin: auto;margin-top: 20px;">
				<view>
					<u-button shape="circle" type="primary" @click="login">
						登录
					</u-button>
				</view>
<!-- 				<view style="margin-top: 20rpx;">
					<u-button shape="circle" type="success" @click="wxLogin(0)">
						微信登录
					</u-button>
				</view> -->
			</view>
		</view>
		<!-- 填写用户信息 -->
<!-- 		<u-popup :show="show" @close="close" mode="center" :round="10" :closeOnClickOverlay="false">
			<view style="width: 90%;height: 600rpx; margin: auto;">
				<view style="height: 100rpx;line-height: 100rpx;text-align: center;
					font-size: 45rpx;font-weight: bold;">
					<text>完善用户信息</text>
				</view>
				<view>
					<u--form :model="user" ref="userForm" :labelWidth="50">
						<u-form-item label="姓名" prop="nickName">
							<u-input v-model="user.nickName" border="bottom" />
						</u-form-item>
						<u-form-item label="手机号" prop="phone">
							<u-input v-model="user.phone" border="bottom" />
						</u-form-item>
					</u--form>
					<view style="margin: auto;margin-top: 20px;width: 80%;">
						<u-button shape="circle" type="primary" @click="submitUser">
							确定
						</u-button>
					</view>
				</view>
			</view>
		</u-popup> -->
	</view>
</template>

<script>
	import api from "../../request/api.js"
	import userApi from "../../request/userApi.js"
	export default {
		data() {
			return {
				type: 0, // 0进入小程序登录，1退出登录后
				userInfo: {},
				show: false,
				form: {
					empno: '',
					password: '',
				},
				formRules: {
					'empno': {
						type: 'string',
						required: true,
						message: '请输入账号',
						trigger: ['blur', 'change']
					},
					'password': {
						type: 'string',
						required: true,
						message: '请输入密码',
						trigger: ['blur', 'change']
					},
				},
				user: {
					nickName: '',
					phone: '',
					openId: ''
				},
				userRules: {
					'nickName': {
						type: 'string',
						required: true,
						message: '请输入姓名',
						trigger: ['blur', 'change']
					},
					'phone': [{
						type: 'string',
						required: true,
						message: '请输入手机号',
						trigger: ['blur', 'change']
					}, {
						pattern: /^1[3-9]\d{9}$/,
						message: '请输入正确的手机号码',
						trigger: ['blur', 'change']
					}]
				}
			}
		},
		onLoad(params) {
			console.log("params=>", params)
			if (params.type) {
				this.type = JSON.parse(params.type)
			}
			if (this.type === 0) {
				// 0为自动登录
				this.wxLogin()
			}
			this.userInfo = uni.getStorageSync("loginUser");
			if (this.userInfo){
				this.form.empno = this.userInfo.nickName;
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.formRules)
			// this.$refs.userForm.setRules(this.userRules)
		},
		methods: {
			// 登录
			login() {
				this.$refs.uForm.validate().then(res => {
					uni.showLoading({
						title: "登录中...",
						mask: true
					})
					api.login(this.form).then(res => {
						// uni.hideLoading();
						if (res.code === 200) {
							//console.log("res.data===>", res.data)
							this.userInfo = res.data.loginUser;
							uni.setStorageSync("OaAuth", res.data.OaAuth)
							uni.setStorageSync("loginUser", res.data.loginUser)
							this.getMenuRoute().then(data =>{
								uni.reLaunch({
									url: "/pages/index/index"
								});
							})
						}
					}).finally(() => {
						uni.hideLoading();
					})
				}).catch(errors => {
					console.log('校验失败')
				})
			},
			// 微信登录
			wxLogin(value) {
				uni.showLoading({
					title: "加载中...",
					mask: true
				})
				// 获取code
				uni.login({
					success: (response) => {
						if (response.errMsg === 'login:ok') {
							const data = {
								code: response.code,
							}
							api.wxLogin(data).then(res => {
								if (res.code === 200) {
									this.userInfo = res.data.loginUser;
									uni.setStorageSync("OaAuth", res.data.OaAuth);
									uni.setStorageSync("loginUser", res.data.loginUser);
									this.getMenuRoute().then(data =>{
										uni.reLaunch({
											url: "/pages/index/index"
										});
									})
								}else {
									console.log(res.message)
								}
							}).finally(() => {
								uni.hideLoading();
							})
						} else {
							uni.hideLoading();
							this.$tip.tip('系统异常，请联系管理员')
						}
					}
				})
			},
			// 获取菜单
			getMenuRoute(){
				return new Promise(resolve =>{
					const data={
						token: uni.getStorageSync("OaAuth")
					}
					api.getMenuRoute(data).then(res =>{
						if (res.code === 200){
							const menuRoute = res.data[0]
							const menuPath = this.initMenuPath(menuRoute);
							uni.setStorageSync("MenuPath",menuPath)
							// console.log("====>",menuPath);
							resolve(menuPath);
						}
					})
				})
			},
			// 获取菜单路由路径
			initMenuPath(menuRoute){
				let menuPath = [];
				if (menuRoute.path !== "") {
					menuPath.push(menuRoute.path);
				}
				if (menuRoute.children.length > 0) {
					menuRoute.children.forEach(item => {
					menuPath = [...menuPath, ...this.initMenuPath(item)];
					});
				}
				return menuPath;
			},
			// 关闭弹窗
			// close() {
			// 	this.show = false
			// },
			// 提交用户信息
			// submitUser() {
			// 	this.$refs.userForm.validate().then(res => {
			// 		uni.showLoading({
			// 			title: "请求中...",
			// 			mask: true
			// 		})
			// 		this.user.openId = this.userInfo.openId;
			// 		userApi.submitUser(this.user).then(res => {
			// 			if (res.code === 200) {
			// 				this.close();
			// 				uni.hideLoading();
			// 				this.userInfo = res.data.loginUser;
			// 				uni.setStorageSync("OaAuth", res.data.OaAuth);
			// 				uni.setStorageSync("loginUser", res.data.loginUser);
			// 				uni.reLaunch({
			// 					url: "/pages/index/index"
			// 				});
			// 			}
			// 		}).finally(() => {
			// 			uni.hideLoading();
			// 		})
			// 	}).catch(errors => {
			// 		console.log('校验失败')
			// 	})
			// }
		},
	}
</script>

<style>
	.login {
		height: 100vh;
	}

	.login-container {
		width: 90%;
		/* 		background-color: pink; */
		margin: auto;
		position: relative;
		top: 15%;
	}
</style>